/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.model.css;

import com.jb.web.bean.BaseComponent;
import com.jb.web.bean.UIComponentImpl;
import com.jb.web.bean.inspector.BackgroundPropertyEditor;
import com.jb.web.bean.inspector.BorderPropertyEditor;
import com.jb.web.bean.inspector.FilterPropertyEditor;
import com.jb.web.bean.inspector.FontPropertyEditor;
import com.jb.web.bean.inspector.OptionPropertyEditor;
import com.jb.web.context.WebContext;
import com.jb.web.model.NoVisualComponent;


public class CssStyle extends UIComponentImpl implements NoVisualComponent {
    static final long serialVersionUID = 123123L;

    static {
        com.jb.web.renderer.RenderKitImpl.registerRenderer(CssStyle.class, CssStyleRenderer.class);
        publish(CssStyle.class, STYLE, "styleClass");
        publish(CssStyle.class, STYLE, "backgroundStyle", new BackgroundPropertyEditor());
        publish(CssStyle.class, STYLE, "borderStyle", new BorderPropertyEditor());
        publish(CssStyle.class, STYLE, "filter", new FilterPropertyEditor());
        publish(CssStyle.class, STYLE, "fontStyle", new FontPropertyEditor());
        publish(CssStyle.class, STYLE, "autoWrap");
        publish(CssStyle.class, STYLE, "floatStyle", new OptionPropertyEditor("seeded:floatStyle"));

        publish(CssStyle.class, STYLE, "display", new OptionPropertyEditor("seeded:displayStyle"));
        publish(CssStyle.class, STYLE, "position", new OptionPropertyEditor("seeded:position"));
        publish(CssStyle.class, STYLE, "top");
        publish(CssStyle.class, STYLE, "left");
        publish(CssStyle.class, STYLE, "height");
        publish(CssStyle.class, STYLE, "width");
        publish(CssStyle.class, STYLE, "overflow", new OptionPropertyEditor("seeded:overflow"));
        publish(CssStyle.class, STYLE, "selector");
        publish(CssStyle.class, STYLE, "personalizable");
//        publish(CssStyle.class, DATA, "value");
    }

    protected String backgroundStyle; //publish
    protected String fontStyle;
    protected boolean autoWrap = true;
    private String borderStyle; //=new BorderStyle();
    private String className;
    private String cursor; //=new BorderStyle();
    private String display;
    private String filter; //=new BorderStyle();
    private String floatStyle;
    private String font;
    private String height; //publish
    private String left;
    private String overflow;
    private String position;
    private String selector;

    //
    private String style;
    private String top;
    private String width; //publish

    public void setAutoWrap(boolean autoWrap) {
        this.autoWrap = autoWrap;
    }

    public boolean isAutoWrap() {
        return autoWrap;
    }

    public void setBackgroundStyle(String backgroundStyle) {
        this.backgroundStyle = backgroundStyle;
    }

    public String getBackgroundStyle() {
        return (backgroundStyle == null) ? "" : backgroundStyle;
    }

    public void setBorderStyle(String borderStyle) {
        this.borderStyle = borderStyle;
    }

    public String getBorderStyle() {
        return (borderStyle == null) ? "" : borderStyle;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getClassName() {
        return className;
    }

    public void setCursor(String cursor) {
        this.cursor = cursor;
    }

    public String getCursor() {
        return cursor;
    }

    public String getDimensionStyle() {
        return Style.addProperty("width", width, null) + Style.addProperty("top", top, null) +
        Style.addProperty("left", left, null) + Style.addProperty("height", height, null);
    }

    public void setDisplay(String display) {
        this.display = display;
    }

    public String getDisplay() {
        return display;
    }

    public void setFilter(String filter) {
        this.filter = filter;
    }

    public String getFilter() {
        return (filter == null) ? "" : filter;
    }

    public void setFloatStyle(String floatStyle) {
        this.floatStyle = floatStyle;
    }

    public String getFloatStyle() {
        return floatStyle;
    }

    public void setFont(String font) {
        this.font = font;
    }

    public String getFont() {
        return font;
    }

    public void setFontStyle(String fontStyle) {
        this.fontStyle = fontStyle;
    }

    public String getFontStyle() {
        return (fontStyle == null) ? "" : fontStyle;
    }

    public void setHeight(String height) {
        this.height = height;
    }

    public String getHeight() {
        return height;
    }

    public void setLeft(String left) {
        this.left = left;
    }

    public String getLeft() {
        return left;
    }

    public void setOverflow(String overflow) {
        this.overflow = overflow;
    }

    public String getOverflow() {
        return overflow;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public String getPosition() {
        return position;
    }

    public String getPositionStyle() {
        return Style.addProperty("display", display, null) + Style.addProperty("position", position, null) +
        Style.addProperty("overflow", overflow, null) + Style.addProperty("float", floatStyle, null);
    }

    public void setSelector(String selector) {
        this.selector = selector;
    }

    public String getSelector() {
        return selector;
    }

    public String getStyle() {
        style = getBorderStyle() + getBackgroundStyle() + getFilter() + getDimensionStyle() + getFontStyle() +
            getPositionStyle() + Style.addProperty("cursor", getCursor(), null);

        return style;
    }

    public void setTop(String top) {
        this.top = top;
    }

    public String getTop() {
        return top;
    }

    public void setWidth(String width) {
        this.width = width;
    }

    public String getWidth() {
        return width;
    }

    public String renderSelector() {
        if ((this.selector != null) && (selector.length() != 0)) {
            return selector + ", ." + getClientId();
        } else {
            return "." + getClientId();
        }
    }

    public void restoreDefault() {
        if (this.copiedFrom != null) {
            this.assign(this.copiedFrom);
        }
    }

    public String toString() {
        return getStyle();
    }

    protected void merge(WebContext pCtx, BaseComponent uiComponent) {
        if (uiComponent == null) {
            return;
        }
        assign(uiComponent);
    }
}
